package com.qianfeng.smartdevice.controller;


//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼            BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  


import com.github.pagehelper.PageInfo;
import com.qianfeng.smartdevice.constants.ResultCode;
import com.qianfeng.smartdevice.dto.R;
import com.qianfeng.smartdevice.dto.UserDto;
import com.qianfeng.smartdevice.exceptions.LoginErrorException;
import com.qianfeng.smartdevice.pojo.User;
import com.qianfeng.smartdevice.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;

/**
 * Created by jackiechan on 2020/12/6 17:36
 *
 * @Author jackiechan
 */
@RestController
@RequestMapping("/users")
@Api(tags = "用户管理模块" )
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/user")
    @ApiOperation("添加用户")
    public R addUser(@RequestBody UserDto userDto) {
        int result = userService.addUser(userDto);
        return R.setOK(null);
    }

    /**
     * 登陆地址
     *
     * @param userDto
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("登陆")
    public R login(@RequestBody UserDto userDto,@ApiIgnore HttpSession session) {
        try {
            User user = userService.login(userDto);
            session.setAttribute("user", user);
            return R.setOK(user);
        } catch (LoginErrorException e) {
            e.printStackTrace();
            return R.SetError(null, "用户名密码错误", ResultCode.ERROR);
        }
    }

    @GetMapping("/detail/{id}")
    @ApiOperation("根据id查询用户")
    public R getUserById(@ApiParam(name = "id",value = "主键id",example = "1") @PathVariable Long id) {
        User user = userService.getUserByUId(id);
        return R.setOK(user);
    }

    @GetMapping("/users")
    @ApiOperation("分页搜索用户")
    public R findAllUsers(@ApiParam(name = "page",value = "第几页",defaultValue = "1")  @RequestParam(defaultValue = "1",required = false) int page,@ApiParam(name = "limit",value = "每页数量",defaultValue = "10",required = false)  @RequestParam(defaultValue = "10") int limit,@ApiParam(name = "status",value = "状态",defaultValue = "-100")  @RequestParam(defaultValue = "-100",required = false) int status,@ApiParam(name = "username", value = "用户模糊名字",example = "zhangsan") @RequestParam(required = false) String username) {
        PageInfo<User> pageInfo = userService.findAllUsers(page, limit,status,username);
        return R.setOK(pageInfo);
    }


    @PutMapping("/user")
    @ApiOperation("更新用户")
    public R updateUser(@RequestBody UserDto userDto) {
        userService.updateUser(userDto);
        return R.setOK(null);
    }

    @DeleteMapping("/users")
    @ApiOperation("根据id删除用户")
    public R deleteUsers(@ApiParam(name = "ids",required = true,value = "主键",type = "query") @RequestParam String[] ids) {
        userService.deleteUsers(ids);
        return R.setOK(null);
    }


}
